package com.hzit.mmcb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzit.mmcb.core.pojo.entity.*;
import com.hzit.mmcb.core.mapper.LendItemReturnMapper;
import com.hzit.mmcb.core.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借回款记录表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@Service
public class LendItemReturnServiceImpl extends ServiceImpl<LendItemReturnMapper, LendItemReturn> implements LendItemReturnService {

    @Autowired
    private LendItemService lendItemService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private LendService lendService;
    @Autowired
    private LendReturnService lendReturnService;
    @Override
    public List<LendItemReturn> listByUserId(Long id, Long userId) {
        List<LendItemReturn> list = this.list(new LambdaQueryWrapper<LendItemReturn>().eq(LendItemReturn::getLendId, id).eq(LendItemReturn::getInvestUserId, userId).orderByAsc(LendItemReturn::getCurrentPeriod));
        return list;
    }

    @Override
    public List<Map<String, Object>> addReturnDetail(Long lendReturnId) {
        //获取还款记录
        LendReturn lendReturn = lendReturnService.getById(lendReturnId);
        //获取标的信息
        Lend lend = lendService.getById(lendReturn.getLendId());
        //回款记录列表
        List<LendItemReturn> list = this.list(new LambdaQueryWrapper<LendItemReturn>().eq(LendItemReturn::getLendReturnId, lendReturnId));
        //返回的map
        List<Map<String, Object>> mapList=new ArrayList<>();
        for (LendItemReturn lendItemReturn : list) {
            //获得投资项
            LendItem lendItem = lendItemService.getById(lendItemReturn.getLendItemId());
            //获取userInfo信息
            UserInfo userInfo = userInfoService.getById(lendItem.getInvestUserId());
            Map<String, Object> map = new HashMap<>();
            map.put("agentProjectCode",lend.getLendNo());
            map.put("voteBillNo",lendItem.getLendItemNo());
            map.put("toBindCode",userInfo.getBindCode());
            map.put("transitAmt",lendItemReturn.getTotal());
            map.put("baseAmt",lendItemReturn.getPrincipal());
            map.put("benifitAmt",lendItemReturn.getInterest());
            map.put("feeAmt", new BigDecimal("0"));
            mapList.add(map);
        }
        return mapList;
    }
}
